package com.ayoka.parenting.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.utils.WeChatManager;
import com.jincp.common.utils.SharedPreferenceUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            WeChatManager.getInstance().mWxApi.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (WeChatManager.getInstance().mWxApi != null) {
            WeChatManager.getInstance().mWxApi.handleIntent(getIntent(), this);
        }
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {
        finish();
    }


    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_SENT_FAILED:
                if (ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX == resp.getType()) {
//                    ("微信分享失败");
                    WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_SHARE_FILED);
                } else if (ConstantsAPI.COMMAND_SENDAUTH == resp.getType()) {
//                    ("微信登录失败");
                    WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_LOGIN_FILED);
                }
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                if (ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX == resp.getType()) {
//                    ("用户取消分享");
                    WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_SHARE_CANCEL);
                } else if (ConstantsAPI.COMMAND_SENDAUTH == resp.getType()) {
//                    ("用户取消登录");
                    WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_LOGIN_CANCEL);
                }
                break;
            case BaseResp.ErrCode.ERR_OK:
                if (ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX == resp.getType()) {
//                    ("微信分享成功");
                    WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_SHARE_SUCCESS);
                } else if (ConstantsAPI.COMMAND_SENDAUTH == resp.getType()) {
//                    ("微信登录成功");
                    String code = ((SendAuth.Resp) resp).code;
                    SharedPreferenceUtil.putString(Constants.WX_AUTHORIZATION_CODE, code);
                    WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_LOGIN_SUCCESS);
                }
                break;
            default:
//                ("微信分享失败");
//                ("微信登录失败");
                WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_SHARE_FILED);
                WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WECHAT_LOGIN_FILED);
                break;
        }
        this.finish();
    }
}